﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using DddCms.Models.UIComponent.Window;
using System.Web.WebPages;

namespace DddCms.Models.UIComponent.Form
{
    public class FormViewModelBuilder
    {
        public FormViewModel FormViewModel { get; set; }


        public FormViewModelBuilder()
        {

            FormViewModel = new FormViewModel();
            FormViewModel.Width = "100%";
        }
        public FormViewModelBuilder MvcForm(Func<MvcForm> form)
        {
            FormViewModel.MvcForm = form;
            return this;
        }
        public FormViewModelBuilder Width(string width)
        {
            FormViewModel.Width = width;
            return this;
        }

        public FormViewModelBuilder Error(string error)
        {
            FormViewModel.Error = error;
            return this;
        }

        public FormViewModelBuilder Title(string title)
        {
            FormViewModel.Title = title;
            return this;
        }

        public FormViewModelBuilder Position(WindowPositionViewModel position)
        {
            FormViewModel.Position = position;
            return this;
        }

        public FormViewModelBuilder Height(int height)
        {
            FormViewModel.Height = height;
            return this;
        }

        public FormViewModelBuilder AddRow(Func<FormRowViewModelBuilder, FormRowViewModelBuilder> rowBuildFunc)
        {
            FormRowViewModelBuilder builderRow = new FormRowViewModelBuilder();
            FormRowViewModel row = rowBuildFunc(builderRow).FormRowViewModel;
            FormViewModel.AddRow(row);

            return this;
        }

        /// <summary>
        /// Create only one field in row
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public FormViewModelBuilder AddField(EditItem value)
        {
            this.AddRow(row => row.AddField(value));
            return this;
        }
        public FormViewModelBuilder AddMessage(string message)
        {
            FormViewModel.AddMessage(message);
            return this;
        }
        public FormViewModelBuilder AddMessage(Func<object, HelperResult> f)
        {
            FormViewModel.AddMessage( f(null).ToHtmlString());
            return this;
        }

        public FormViewModel Build()
        {
            return FormViewModel;
        }

    }
}